package com.aidong.service;

/**
 * Created by john on 16-11-2.
 */
import com.aidong.common.JobConsts;
import com.aidong.dao.wills.ShopDprDao;
import com.aidong.model.wills.DayDprModel;
import com.aidong.util.DateUtil;
import com.aidong.util.ExportUtil;
import com.aidong.util.FileCheckUtil;
import com.aidong.util.ReportUtil;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class DayDprService extends BaseJobService {
    protected Logger log = Logger.getLogger(DayDprService.class);
    @Resource
    ShopDprDao shopDprDao;
    @Resource
    ReportUtil reportUtil;

    public DayDprService() {
    }

    public synchronized void doJob() {
        Thread th1 = new Thread() {
            public void run() {
                try {
                    try {
                        DayDprService.this.doDayDpr();
                    } catch (Exception var5) {
                        var5.printStackTrace();
                    }

                } finally {
                    ;
                }
            }
        };
        th1.start();
    }

    private void doDayDpr() throws Exception {
        try {
            Timestamp e = DateUtil.getCurrentTimestamp();
            String cur = DateUtil.formatDate(e, DateUtil.DATE_PATTERN_YYYYMMDD);
            String curDate = DateUtil.addDays(cur, -1);
            HashMap commonData = new HashMap();
            commonData.put("dateFrom", curDate);
            commonData.put("dateTo", curDate);
            Date tmpDate = DateUtil.formatDate(curDate);
            HashMap data1 = new HashMap();
            ArrayList listAll = new ArrayList();
            List list = this.shopDprDao.getDprDataOfList(tmpDate, tmpDate);
            listAll.addAll(list);
            data1.putAll(commonData);
            data1.put("list", listAll);
            List list2 = this.shopDprDao.getDprDataOfTotal(tmpDate, tmpDate);
            ArrayList memlist = new ArrayList();
            ArrayList ptlist = new ArrayList();
            Iterator fileName = list2.iterator();

            while(true) {
                if(!fileName.hasNext()) {
                    data1.put("memlist", memlist);
                    data1.put("ptlist", ptlist);
                    String fileName1 = curDate + "_" + JobConsts.TEMPLATE_NAME_DPR_INCOME + JobConsts.EXCEL_SUFFIX;
                    String fileChName1 = curDate + "_" + JobConsts.CH_NAME_DPR_INCOME_DAY + JobConsts.EXCEL_SUFFIX;
                    String filePath = this.getDownloadPath() + curDate + "\\" + JobConsts.TEMPLATE_NAME_DPR_INCOME;
                    String fileDbPath = curDate + "\\" + JobConsts.TEMPLATE_NAME_DPR_INCOME;
                    FileCheckUtil.checkFilePath(filePath);
                    ExportUtil.exportExcel(filePath, fileName1, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_DPR_INCOME + JobConsts.EXCEL_SUFFIX, data1);
                    this.reportUtil.addReportFile("06", fileName1, fileChName1, fileDbPath, curDate, "ALL");
                    break;
                }

                DayDprModel fileChName = (DayDprModel)fileName.next();
                if("1".equals(fileChName.getAreaType())) {
                    memlist.add(fileChName);
                } else if("2".equals(fileChName.getAreaType())) {
                    ptlist.add(fileChName);
                }
            }
        } catch (Exception var16) {
            var16.printStackTrace();
            this.log.error("生成(DPR收入日)失败");
            return;
        }

        this.log.info("生成(DPR收入日)成功");
    }

    public void createDayDpr(String curDate) throws Exception {
        try {
            HashMap e = new HashMap();
            e.put("dateFrom", curDate);
            e.put("dateTo", curDate);
            Date tmpDateFrom = DateUtil.formatDate(curDate);
            Date tmpDateTo = DateUtil.formatDate(curDate);
            HashMap data1 = new HashMap();
            ArrayList listAll = new ArrayList();
            List list = this.shopDprDao.getDprDataOfList(tmpDateFrom, tmpDateTo);
            listAll.addAll(list);
            data1.putAll(e);
            data1.put("list", listAll);
            List list2 = this.shopDprDao.getDprDataOfTotal(tmpDateFrom, tmpDateTo);
            ArrayList memlist = new ArrayList();
            ArrayList ptlist = new ArrayList();
            Iterator fileName = list2.iterator();

            while(true) {
                if(!fileName.hasNext()) {
                    data1.put("memlist", memlist);
                    data1.put("ptlist", ptlist);
                    String fileName1 = curDate + "_" + JobConsts.TEMPLATE_NAME_DPR_INCOME + JobConsts.EXCEL_SUFFIX;
                    String fileChName1 = curDate + "_" + JobConsts.CH_NAME_DPR_INCOME_DAY + JobConsts.EXCEL_SUFFIX;
                    String filePath = this.getDownloadPath() + curDate + "\\" + JobConsts.TEMPLATE_NAME_DPR_INCOME;
                    String fileDbPath = curDate + "\\" + JobConsts.TEMPLATE_NAME_DPR_INCOME;
                    FileCheckUtil.checkFilePath(filePath);
                    ExportUtil.exportExcel(filePath, fileName1, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_DPR_INCOME + JobConsts.EXCEL_SUFFIX, data1);
                    this.reportUtil.addReportFile("06", fileName1, fileChName1, fileDbPath, curDate, "ALL");
                    break;
                }

                DayDprModel fileChName = (DayDprModel)fileName.next();
                if("1".equals(fileChName.getAreaType())) {
                    memlist.add(fileChName);
                } else if("2".equals(fileChName.getAreaType())) {
                    ptlist.add(fileChName);
                }
            }
        } catch (Exception var15) {
            var15.printStackTrace();
            this.log.error("生成(DPR收入日)失败");
            return;
        }

        this.log.info("生成(DPR收入日)成功");
    }
}
